package com.jizhi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jizhi.entity.Drugdictionary;
import com.jizhi.entity.Drugstore;
import com.jizhi.entity.Jilu;
import com.jizhi.mapper.DrugstoreMapper;
import com.jizhi.mapper.JiluMapper;
import com.jizhi.service.DrugstoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 王书豪
 * @since 2020-11-17
 */
@Service
public class DrugstoreServiceImpl extends ServiceImpl<DrugstoreMapper, Drugstore> implements DrugstoreService {
    @Resource
    private DrugstoreMapper drugstoreMapper;
    //注入一个记录表的mapper
    @Resource
    private JiluMapper jiluMapper;
    @Override
    public int insert(Drugstore drugstore,int number,String type,String jingbanren) {
        //添加一条药品仓库信息
        int num= drugstoreMapper.insert(drugstore);
        //当药品仓库添加成功后添加一条出入库记录
        if (num!=0){
            //创建一个出入库记录表的实体对象
            Jilu jilu = new Jilu();
            jilu.setJiluname(drugstore.getDrugstoreName());
            jilu.setJilutime(new Date());//new一个当前时间
            jilu.setJilunumber(number);
            jilu.setJilutype(type);
            jilu.setJilupeople(jingbanren);
            int sj=(int)(10000+Math.random()*(99999-10000+1));//一个10000到99999的随机数
            jilu.setJilupihao(String.valueOf(sj));
            //添加一条出入库记录
            int sum= jiluMapper.insert(jilu);
        }
        return num;
    }

    @Override
    public List<Drugstore> selectTjcx(Page page) {
        QueryWrapper<Drugstore> wrapper=new QueryWrapper<>();
        wrapper.le("drugstorenum",30);
        IPage<Drugstore> drugdictionaryIPage = drugstoreMapper.selectPage(page,wrapper);
        return drugdictionaryIPage.getRecords();
    }
}
